package org.lex.notepad.ui.search;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.text.Position;


import org.lex.notepad.Notepad;
import org.lex.swing.Swings;
import org.lex.swing.widgets.EnhancedDialog;

public class FindDialog extends EnhancedDialog {
	private JPanel pane = null;
	private JLabel findLabel = null;
	private JTextField findTextField = null;
	private JButton findButton = null;
	private JCheckBox matchWholeWordCheckBox = null;
	private JCheckBox matchCaseCheckBox = null;
	private JRadioButton searchUpRadioButton = null;
	private JRadioButton searchDownRadioButton = null;
	private JButton closeButton = null;
	private JPanel gluePane = null;
	// ==============================
	private TextFinder finder = null;

	public FindDialog() {
		super(Notepad.getApplication().getRootWindow());
		this.finder = new TextFinder(Notepad.getApplication().getEditor());
		this.initialize();
		this.setDefaultButton(this.getFindButton());
	}

	public void cancel() {
		dispose();
	}

	class FindNextActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String target = getFindTextField().getText();
			if (0 == target.length())
				return;
			finder.setTarget(target);
			boolean b = finder.findNext();
			if (false == b)
				JOptionPane.showMessageDialog(FindDialog.this, "Could not find \"" + target + "\"", "Not found", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	private void initialize() {
		this.setSize(335, 200);
		this.setTitle("Find");
		this.setContentPane(getPane());
	}

	private JPanel getPane() {
		if (pane == null) {
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.gridwidth = 4;
			gridBagConstraints7.fill = GridBagConstraints.BOTH;
			gridBagConstraints7.weightx = 10.0;
			gridBagConstraints7.weighty = 10.0;
			gridBagConstraints7.gridy = 4;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 2;
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.insets = new Insets(0, 5, 11, 11);
			gridBagConstraints6.gridy = 5;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.insets = new Insets(0, 6, 5, 11);
			gridBagConstraints4.gridwidth = 2;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 3;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(5, 6, 0, 11);
			gridBagConstraints3.gridwidth = 2;
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 2;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.anchor = GridBagConstraints.WEST;
			gridBagConstraints21.insets = new Insets(0, 12, 5, 0);
			gridBagConstraints21.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints21.gridy = 3;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.anchor = GridBagConstraints.WEST;
			gridBagConstraints11.insets = new Insets(5, 12, 0, 0);
			gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.gridy = 2;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 2;
			gridBagConstraints2.insets = new Insets(0, 5, 0, 11);
			gridBagConstraints2.fill = GridBagConstraints.NONE;
			gridBagConstraints2.gridy = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 10.0;
			gridBagConstraints1.gridwidth = 2;
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new Insets(0, 12, 0, 0);
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridwidth = 3;
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.insets = new Insets(12, 12, 3, 11);
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.gridy = 0;
			findLabel = new JLabel();
			findLabel.setText("Find what");
			pane = new JPanel();
			pane.setLayout(new GridBagLayout());
			pane.add(findLabel, gridBagConstraints);
			pane.add(getFindTextField(), gridBagConstraints1);
			pane.add(getFindButton(), gridBagConstraints2);
			pane.add(getMatchWholeWordCheckBox(), gridBagConstraints11);
			pane.add(getMatchCaseCheckBox(), gridBagConstraints21);
			pane.add(getSearchUpRadioButton(), gridBagConstraints3);
			pane.add(getSearchDownRadioButton(), gridBagConstraints4);
			pane.add(getCloseButton(), gridBagConstraints6);
			pane.add(getGluePane(), gridBagConstraints7);
			// =============================================
			Swings.createButtonGroup(this.getSearchUpRadioButton(), this.getSearchDownRadioButton());
		}
		return pane;
	}

	private JTextField getFindTextField() {
		if (findTextField == null) {
			findTextField = new JTextField();
		}
		return findTextField;
	}

	private JButton getFindButton() {
		if (findButton == null) {
			findButton = new JButton();
			findButton.setText("Find Next");
			findButton.addActionListener(new FindNextActionListener());
		}
		return findButton;
	}

	private JCheckBox getMatchWholeWordCheckBox() {
		if (matchWholeWordCheckBox == null) {
			matchWholeWordCheckBox = new JCheckBox();
			matchWholeWordCheckBox.setText("Match whole word only");
		}
		return matchWholeWordCheckBox;
	}

	private JCheckBox getMatchCaseCheckBox() {
		if (matchCaseCheckBox == null) {
			matchCaseCheckBox = new JCheckBox();
			matchCaseCheckBox.setText("Match case");
		}
		return matchCaseCheckBox;
	}

	private JRadioButton getSearchUpRadioButton() {
		if (searchUpRadioButton == null) {
			searchUpRadioButton = new JRadioButton();
			searchUpRadioButton.setText("Search up");
			searchUpRadioButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					finder.setDirection(Position.Bias.Backward);
				}
			});
		}
		return searchUpRadioButton;
	}

	private JRadioButton getSearchDownRadioButton() {
		if (searchDownRadioButton == null) {
			searchDownRadioButton = new JRadioButton();
			searchDownRadioButton.setText("Search down");
			searchDownRadioButton.setSelected(true);
			searchDownRadioButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					finder.setDirection(Position.Bias.Forward);
				}
			});
		}
		return searchDownRadioButton;
	}

	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton();
			closeButton.setText("Close");
			closeButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					cancel();
				}
			});
		}
		return closeButton;
	}

	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
		}
		return gluePane;
	}
}
